var vm = new Vue({
    el:"#app",
    data(){
        return {
            //搜索条件
            JxsNameOrNo:'',
            formItem: {
                DealerNo: '',
                Name:'',
                Remark:''
            },
            EditFormItem:{
                DealerNo: '',
                Name:'',
                Remark:''
            },
            modal1:false, //添加经销商弹框
            modal2:false, //编辑经销商弹框
            //搜索结果
            columns1: [
                {
                    type: 'index',
                    title: '序号',
                    width:46,
                    align: 'center',
                },
                {
                    title: '经销商编号',
                    key: 'DealerNo',
                    align: 'center',
                },
                {
                    title: '经销商名称',
                    key: 'Name',
                    align: 'center',
                    width:300
                },
                {
                    title: '备注',
                    key: 'Remark',
                    align: 'center',
                },
                {   title: '操作',
                    key: 'action',
                    align: 'center',
                    width:150,
                    render: (h, params) => {
                        return h('div', [
                            h('a', {
                                style: {
                                    marginRight: '5px',
                                    border : '0',
                                    fontSize: '14px',
                                },
                                on: {
                                    click: () => {
                                        this.EditFormItem.DealerNo = params.row.DealerNo;
                                        this.EditFormItem.Name = params.row.Name;
                                        this.EditFormItem.Remark = params.row.Remark;
                                        this.modal2=true;
                                    }
                                }
                            },'编辑'),
                            h('span', {
                            },'|'),
                            h('a', {
                                style: {
                                    marginLeft: '5px',
                                    border : '0',
                                    fontSize: '14px',
                                },
                                on: {
                                    click: () => {
                                        var DealerNo = params.row.DealerNo;
                                        this.confirm(DealerNo);
                                    }
                                }
                            },'删除')
                        ])
                    }
                }
            ],
            data1: [],

        }
    },
    created(){
        this.initThisPage();
    },
    methods:{
        ok () {
        },
        cancel () {
        },
        confirm (DealerNo) {
            var self = this;
            this.$Modal.confirm({
                title: '提示',
                content: '<p>是否删除</p>',
                onOk: () => {
                    self.delDealer(DealerNo);
                },
                onCancel: () => {
                    toastr.info("取消删除");
                }
            });
        },
        //打开添加经销商弹框
        openModel1(){
            this.formItem.DealerNo = "JXS-"+this.uuid(8, 16);
            this.modal1 = true;
        },
        querytable(){
            var self = this;
            var info ;
            var JxsNameOrNo = self.JxsNameOrNo;
            var jsondata={"JxsNameOrNo":JxsNameOrNo};
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goJxs/queryAllJxs", //请求的url地址
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                async:false,
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    info = eval(data.ResultList);
                },
                error: function() {},
            });
            if(info){
                self.data1 = info;
            }
        },
        saveDealer(){
            var self = this;
            var DealerNo = self.formItem.DealerNo;
            var Name = self.formItem.Name;
            Name = Name.trim();
            var Remark = self.formItem.Remark;
            if(Name==null||Name==""){
                toastr.warning("请录入经销商姓名");
                return false;
            }
            var jsondata = {
                "DealerNo":DealerNo,
                "Name":Name,
                "Remark":Remark
            }
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goJxs/saveDealer", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                type: "POST", //请求方式
                headers : {
                    'Content-Type' : 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.Status=='1'){
                        toastr.success('保存成功');
                        self.formItem.DealerNo = "";
                        self.formItem.Name = "";
                        self.formItem.Remark = "";
                        self.querytable();
                    }else if(data.Status=='-1'){
                        toastr.warning('保存失败，经销商已存在');
                    }else{
                        toastr.error('保存失败，请重新录入');
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function(data) {

                }
            });
        },
        // 编辑弹框
        editDealer(){
            var self = this;
            var DealerNo = self.EditFormItem.DealerNo;
            var Name = self.EditFormItem.Name;
            Name = Name.trim();
            var Remark = self.EditFormItem.Remark;
            if(Name==null||Name==""){
                toastr.warning("请录入经销商姓名");
                return false;
            }
            var jsondata = {
                "DealerNo":DealerNo,
                "Name":Name,
                "Remark":Remark
            }
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goJxs/editDealer", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                type: "POST", //请求方式
                headers : {
                    'Content-Type' : 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.Status=='1'){
                        toastr.success('编辑成功');
                        self.querytable();
                    }else if(data.Status=='-1'){
                        toastr.warning('编辑失败，经销商已存在');
                    }else{
                        toastr.error('编辑失败，请重新编辑');
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function(data) {

                }
            });
        },
        delDealer(DealerNo){
            var self = this;
            var jsondata = {
                "DealerNo":DealerNo,
            }
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goJxs/delDealer", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                type: "POST", //请求方式
                headers : {
                    'Content-Type' : 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.Status=='1'){
                        toastr.success("删除成功");
                        self.querytable();
                    }else{
                        toastr.error('删除失败，请刷新页面后重试');
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function(data) {

                }
            });
        },
        initThisPage(){
            var self = this;
            $.ajax({
                url: "./goLogin/initIndexPage", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                async:false,
                dataType: "json", //返回格式为json
                type: "GET", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.Status==0){
                        // top.location.href = "../../login.html";
                        window.location.href="../../login.html";
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function() {}
            });
        },

        //手机号验证
        isPoneAvailable(str) {
            var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
            if (!myreg.test(str)) {
                return false;
            } else {
                return true;
            }
        },
        //uuid
        uuid(len, radix) {
            var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
            var uuid = [], i;
            radix = radix || chars.length;

            if (len) {
                // Compact form
                for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
            } else {
                // rfc4122, version 4 form
                var r;

                // rfc4122 requires these characters
                uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
                uuid[14] = '4';

                // Fill in random data. At i==19 set the high bits of clock sequence as
                // per rfc4122, sec. 4.1.5
                for (i = 0; i < 36; i++) {
                    if (!uuid[i]) {
                        r = 0 | Math.random()*16;
                        uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
                    }
                }
            }

            return uuid.join('');
        }
    }
})